package ace.module.account.core.impl.controller.rest.admin.admin;

import ace.cmp.core.model.R;
import ace.cmp.security.oauth2.resource.server.core.annotation.PreAuthorizeIsAuthenticated;
import ace.module.account.api.constant.AccountAuthorityConstant;
import ace.module.account.api.model.input.IamAccountModifyStatusInput;
import ace.module.account.api.model.input.account.IamAdminAccountAdminSaveInput;
import ace.module.account.api.model.input.account.IamAdminAccountAdminSearchInput;
import ace.module.account.core.impl.manager.IamAccountManager;
import ace.module.account.core.impl.model.dto.IamAdminAccountListItemDto;
import ace.module.account.core.impl.service.IamAccountService;
import ace.module.common.api.model.input.GenericInput;
import ace.module.common.api.model.input.GenericListInput;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 管理员账户API.
 *
 * @author Caspar
 *
 */
@Tag(name = "管理员账户-管理-API")
@PreAuthorizeIsAuthenticated
@AllArgsConstructor

@RestController
@Valid
public class IamAdminAccountAdminController {

  public static final String PATH_BASE = "/account/iam-account/admin/admin";
  public static final String PATH_SEARCH = "/search";
  public static final String PATH_DETAIL = "/detail";
  public static final String PATH_SAVE = "/save";
  public static final String PATH_REMOVE = "/remove";
  public static final String PATH_MODIFY_STATUS = "/modifyStatus";
  public static final String URI_SEARCH = PATH_BASE + PATH_SEARCH;
  public static final String URI_DETAIL = PATH_BASE + PATH_DETAIL;
  public static final String URI_SAVE = PATH_BASE + PATH_SAVE;
  public static final String URI_REMOVE = PATH_BASE + PATH_REMOVE;
  public static final String URI_MODIFY_STATUS = PATH_BASE + PATH_MODIFY_STATUS;

  private final IamAccountService iamAccountService;
  private final IamAccountManager iamAccountManager;

  @PreAuthorize("@securityService.hasAuthority('" + AccountAuthorityConstant.IAM_ACCOUNT_ADMIN_READ + "')")
  @Operation(summary = "管理员账户搜索", description = "权限:" + AccountAuthorityConstant.IAM_ACCOUNT_ADMIN_READ)
  @GetMapping(URI_SEARCH)
  public R<Page<IamAdminAccountListItemDto>> search(@Valid IamAdminAccountAdminSearchInput input, @PageableDefault Pageable pageable) {
    Page<IamAdminAccountListItemDto> result = iamAccountService.adminSearch(input, pageable);

    return R.ok(result);
  }

  @PreAuthorize("@securityService.hasAuthority('" + AccountAuthorityConstant.IAM_ACCOUNT_ADMIN_READ + "')")
  @Operation(summary = "管理员账户明细", description = "权限:" + AccountAuthorityConstant.IAM_ACCOUNT_ADMIN_READ)
  @GetMapping(URI_DETAIL)
  public R<Page<IamAdminAccountListItemDto>> detail(@Valid IamAdminAccountAdminSearchInput input, @PageableDefault Pageable pageable) {
    Page<IamAdminAccountListItemDto> result = iamAccountService.adminSearch(input, pageable);
    return R.ok(result);
  }

  @PreAuthorize("@securityService.hasAuthority('" + AccountAuthorityConstant.IAM_ACCOUNT_ADMIN_WRITE + "')")
  @Operation(summary = "管理员账户新增修改", description = "返回id,权限:" + AccountAuthorityConstant.IAM_ACCOUNT_ADMIN_WRITE + "")
  @GetMapping(URI_SAVE)
  public R<Long> save(@RequestBody GenericInput<IamAdminAccountAdminSaveInput> input) {
    Long result = iamAccountService.adminSave(input.getData());
    return R.ok(result);
  }

  @Operation(summary = "删除", description = "权限:" + AccountAuthorityConstant.IAM_ACCOUNT_ADMIN_REMOVE)
  @PreAuthorize("@securityService.hasAuthority('" + AccountAuthorityConstant.IAM_ACCOUNT_ADMIN_REMOVE + "')")
  @PostMapping(URI_REMOVE)
  public R<Void> remove(@RequestBody GenericListInput<Long> input) {
    iamAccountManager.removeLogic(input.getData());
    return R.ok();
  }

  @Operation(summary = "启用/禁用状态", description = "权限:" + AccountAuthorityConstant.IAM_ACCOUNT_ADMIN_WRITE)
  @PreAuthorize("@securityService.hasAuthority('" + AccountAuthorityConstant.IAM_ACCOUNT_ADMIN_WRITE + "')")
  @PostMapping(URI_MODIFY_STATUS)
  public R<Void> modifyStatus(@RequestBody GenericListInput<IamAccountModifyStatusInput> input) {
    iamAccountService.modifyStatus(input.getData());
    return R.ok();
  }
}
